import { Component, OnInit, Injector } from '@angular/core';
import { NgZorroPageBaseComponent } from '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { SignalRService } from '../../core/service/singnalr-service';
import { BaseComponentConfig, ArSystemFormControl, FieldType, SystemFormControl, LoadRemoteDataType, AuthService } from '../..';

@Component({
  selector: 'app-user-content',
  templateUrl: './user-content.component.html',
  styleUrls: ['./user-content.component.scss']
})
export class UserContentComponent extends NgZorroPageBaseComponent implements OnInit {

  isPwdEdit = '0';
  userName = '';
  isPending = false;
  constructor(injector: Injector,
    public Auth: AuthService,
    private signalrService: SignalRService) {
    super(injector,
      new BaseComponentConfig(
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        ""),
      new ArSystemFormControl([
        new SystemFormControl("photoUrl", "头像", FieldType.Text, true, 30, 0, "", "", false, null, LoadRemoteDataType.LoadAll, true, true, false),
        new SystemFormControl("personName", "姓名", FieldType.Text, false, 0, 0, "", "", false, null, LoadRemoteDataType.LoadAll, true, true, false),
        new SystemFormControl("phoneNumber", "电话", FieldType.Datetime, false, 30, 0, "", "", false, null, LoadRemoteDataType.LoadAll, true, false, false),
        new SystemFormControl("userPwd", "演练状态", FieldType.Text, false, 0, 0, "", "", false, null, LoadRemoteDataType.LoadAll, true, true, false),
        new SystemFormControl("userPwd1", "演练状态", FieldType.Text, false, 0, 0, "", "", false, null, LoadRemoteDataType.LoadAll, true, true, false),
        new SystemFormControl("userPwd2", "演练状态", FieldType.Text, false, 0, 0, "", "", false, null, LoadRemoteDataType.LoadAll, true, true, false),
      ])
    );
    this.listEntities.pageSize = 5;
  }

  ngOnInit() {
    this.IniObserver.subscribe(e => {
      this.getUserInfo();
    });
  }


  /**
   * 获取空的实例
   */
  getNewEntity(): any {
    return {
      photoUrl: '',
      personName: '',
      phoneNumber: '',
      userPwd: '',
      userPwd1: '',
      userPwd2: ''
    };
  }

  async saveInfo() {
    try {
      if (this.isPwdEdit === '0') {
        // 修改用户信息
        if (!this.focusEntity.personName) {
          this.message.error('请填写姓名');
        } else if (!this.focusEntity.phoneNumber) {
          this.message.error('请填写用户电话');
        } else {
          this.isPending = true;
          await this.httpService.post<any>('SystemUser/EditPersonInfo', {
            PersonName: this.focusEntity.personName,
            PhoneNumber: this.focusEntity.phoneNumber,
            PhotoUrl: this.focusEntity.photoUrl,
          });
          this.message.success('修改成功');
          await this.getUserInfo();
        }
      } else {
        // 修改用户密码
        if (!this.focusEntity.userPwd) {
          this.message.error('请填写旧密码');
        } else if (!this.focusEntity.userPwd1 || !this.focusEntity.userPwd2 || this.focusEntity.userPwd1 !== this.focusEntity.userPwd2) {
          this.message.error('请设新密码，且新密码两次输入要一致！');
        } else {
          this.isPending = true;
          await this.httpService.post('SystemUser/EditPwd', { UserId: this.Auth.token.userid, OldPwd: this.focusEntity.userPwd, NewPwd: this.focusEntity.userPwd1 });
          this.message.success('密码修改成功');
          await this.getUserInfo();
        }
      }

    } finally {
      this.isPending = false;
    }

  }

  async getUserInfo() {
    const reslut = await this.httpService.get<any>('SystemUser/GetUserInformation?input=' + this.Auth.token.userid);
    this.userName = reslut.userName;
    this.focusEntity = {
      photoUrl: reslut.photoUrl,
      personName: reslut.personName,
      phoneNumber: reslut.phoneNumber,
      userPwd: '',
      userPwd1: '',
      userPwd2: ''
    };
  }
}
